package com.jiaozi.core.security.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiaozi.base.utils.bean.enums.REnum;
import com.jiaozi.core.exception.CommonException;
import com.jiaozi.core.security.pojo.LoginUserDetails;
import com.jiaozi.core.security.pojo.login.LuUserVO;
import com.jiaozi.system.entity.SysUser;
import com.jiaozi.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户登录操作
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;
    @Override
    public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取用户信息
        SysUser sysUser = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, username));
        if(sysUser==null){
            throw new CommonException(REnum.LOGIN_USERNAME_ERROR,"");
        }
        LuUserVO luUserVO= BeanUtil.copyProperties(sysUser, LuUserVO.class);
        luUserVO.setUserId(sysUser.getId().toString());
        LoginUserDetails loginUserDetails =new LoginUserDetails(luUserVO);
        return loginUserDetails;
    }
}

